package system.process;

import java.sql.ResultSet;
import java.sql.SQLException;

import system.logger.ClusterLogger;
import system.logic.bo.BaseBo;
import system.db.BaseDb;
import system.db.DBTableCreator;
import system.db.DBTools;

public class ProcessBo implements DBTableCreator, BaseBo
{
    private final static ClusterLogger log = ClusterLogger.getLogger(ProcessBo.class.getName());
    
    public static final String TB_NAME = "process";
    
    public static final String CLM_PRCNAME = "PrcName";
    public static final String CLM_PRCSTATE = "PrcState";
    public static final String CLM_PRCCOMMENT = "PrcComment";
    public static final String CLM_PRCTYPE = "PrcType";
    
    public static final int STATE_TERMINATED = 0;
    public static final int STATE_RUNNING = 1;
    public static final int STATE_PAUSED = 2;
    public static final int STATE_ERROR = 3;
    
    public static final int TYPE_USER = 0;
    public static final int TYPE_SYSTEM = 1;
    
    private String prcName = null;
    private int prcState = 0;
    private String prcComment = null;
    private int prcType = 0;
    
    
    public ProcessBo()
    {
        
    }
    
    public ProcessBo(String processName)
    {
        this.prcName = processName;
        
        dbFetch();
    }
    
	@Override
	public void onDbCreate() throws Exception 
	{
		String sql = "CREATE TABLE " + TB_NAME
			+ " (" + CLM_PRCNAME + " char(50), " 
			+ CLM_PRCSTATE + " integer,"
			+ CLM_PRCCOMMENT + " char(100)," 
			+ CLM_PRCTYPE + " integer)";
		
		BaseDb.getInstance().dbExecuteStatement(sql);
	}

    @Override
    public void dbDelete( )
    {
        if(this.prcName != null && !this.prcName.isEmpty())
        {
            try
            {
                BaseDb.getInstance().dbExecuteStatement("DELETE * FROM " + TB_NAME + " WHERE " + 
                    CLM_PRCNAME + " = " + DBTools.quote(prcName));
            }
            catch(Exception e)
            {
                log.error("Could not delete process " + DBTools.quote(prcName) + " from database!");
                log.error(e.getMessage());
                log.error(e.getStackTrace().toString());
            }
        }
    }

    @Override
    public void dbFetch( )
    {
        if(this.prcName != null && !this.prcName.isEmpty())
        {
            ResultSet rs = null;
            try
            {
                rs = BaseDb.getInstance().dbQuery("SELECT * FROM " + TB_NAME + " WHERE "
                    + CLM_PRCNAME + " = " + DBTools.quote(this.prcName));
                
                if(rs.next())
                {
                    this.prcState = rs.getInt(2);
                    this.prcComment = rs.getString(3);
                    this.prcType = rs.getInt(4);
                }
                else
                {
                    this.prcState = 0;
                    this.prcComment = "";
                    this.prcType = 0;
                }
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
            finally
            {
                if(rs != null)
                    try
                    {
                        rs.close();
                    }
                    catch(SQLException e)
                    {
                        e.printStackTrace();
                    }
            }
        }        
    }

    @Override
    public void dbInsert( )
    {
        if(this.prcName != null && !this.prcName.isEmpty())
        {
            try
            {
                BaseDb.getInstance().dbExecuteStatement(
                    "INSERT INTO " + TB_NAME +
                    " VALUES (" + DBTools.quote(prcName) +  ", " + prcState + 
                    ", " + DBTools.quote(prcComment) + ", " + prcType + ")");
            }
            catch(Exception e)
            {
                log.error("Could not insert process " + DBTools.quote(prcName) + " into database!");
                log.error(e.getMessage());
                log.error(e.getStackTrace().toString());
            }
        }        
    }

    public void setPrcName(String prcName)
    {
        this.prcName = prcName;
    }

    public int getPrcState( )
    {
        return prcState;
    }

    public void setPrcState(int prcState)
    {
        this.prcState = prcState;
    }

    public String getPrcComment( )
    {
        return prcComment;
    }

    public void setPrcComment(String prcComment)
    {
        this.prcComment = prcComment;
    }

    public int getPrcType( )
    {
        return prcType;
    }

    public void setPrcType(int prcType)
    {
        this.prcType = prcType;
    }

    public String getPrcName( )
    {
        return prcName;
    }

    @Override
    public void dbUpdate( )
    {
        if(this.prcName != null && !this.prcName.isEmpty())
        {
            ResultSet rs = null;
            try
            {
                rs = BaseDb.getInstance().dbQuery("SELECT * FROM " + TB_NAME + " WHERE "
                    + CLM_PRCNAME + " = " + DBTools.quote(this.prcName));
            
                if(!rs.next())
                {
                    this.dbInsert();
                }
                else
                {
                    BaseDb.getInstance().dbExecuteStatement(
                        "Update " + TB_NAME +
                        " SET " +  CLM_PRCSTATE + " = " + prcState + 
                        ", " + CLM_PRCCOMMENT + " = " + DBTools.quote(prcComment) + 
                        ", " + CLM_PRCTYPE + " = " + prcType
                        + " WHERE " + CLM_PRCNAME + " = " + DBTools.quote(prcName));
                }
            
            }
            catch(Exception e)
            {
                log.error("Could not update process " + DBTools.quote(prcName) + " into database!");
                log.error(e.getMessage());
                log.error(e.getStackTrace().toString());
            }
            finally
            {
                if(rs != null)
                    try
                    {
                        rs.close();
                    }
                    catch(SQLException e)
                    {
                        e.printStackTrace();
                    }
            }
        }
        
    }

}
